import requests
from bs4 import BeautifulSoup

url='https://www.lgfdcw.com/cs/'

headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
r=requests.get(url,headers=headers)

with open('ld.html','w',encoding='gbk') as f:
    f.write(r.content.decode('gbk'))

soup= BeautifulSoup(r.content.decode('gbk'),'lxml')
result=soup.find_all('tr',{'bgcolor':'#FFFFFF'})
r1=soup.find_all('tr',{'bgcolor':'#FFFFFF'})
# print(r1)
list1=[]
for e in r1:
    h={}
    h['详细地址']=e.find('a',{'target':'_blank'}).string
    h['详情链接']=url + '/' + e.find('a',{'target':'_blank'})['href']
    h['房型']=e.select('td')[2].string
    h['户型'] = e.select('td')[3].string
    h['面积'] = e.select('td')[4].string
    h['出售价格'] = e.select('td')[5].string
    h['登记时间'] = e.select('td')[6].string

    list1.append(h)
print(list1)
with open('data1.txt','w',encoding='utf-8') as f:
    f.write(list1.__str__())
# for e in result:
#     print(e.get_text())
